package cc.orange.utils.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.DigitsKeyListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import cc.orange.R;
import cc.orange.utils.ZXToastUtil;

/**设置账号
 */

public class PopupDiscoView {

    private static View inflate;
    private static PopupWindow popupWindow;
    private static int type;
    private static String trim_num = "";
    public static void showPopupwindow(final Context context,int setTtype,String se,
                                       View view,PopuStatus pp) {//回调

        if (popupWindow == null && inflate == null) {
            inflate = LayoutInflater.from(context).inflate(R.layout.item_discover_popup, null);
            popupWindow = new PopupWindow(inflate, RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
        }
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);

        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        //设置位
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        backgroundAlpha(0.3f, (Activity) context);

//        popupWindow.showAsDropDown(view);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f,(Activity) context);
            }
        });

        type = 1;
        ImageView item_disc_rel2 = inflate.findViewById(R.id.item_disc_rel2);
        EditText item_disc_popup_eidt1 = inflate.findViewById(R.id.item_disc_popup_eidt1);
        EditText item_disc_popup_eidt2 = inflate.findViewById(R.id.item_disc_popup_eidt2);

        TextView item_disc_popup_text1 = inflate.findViewById(R.id.item_disc_popup_text1);//取消
        TextView item_disc_popup_text2 = inflate.findViewById(R.id.item_disc_popup_text2);//确认
        TextView item_disc_popup_text3 = inflate.findViewById(R.id.item_disc_popup_text3);//注销账号

        TextView item_disc_rel1_text1 = inflate.findViewById(R.id.item_disc_rel1_text1);//设置你的账号
        TextView item_disc_rel1_text2 = inflate.findViewById(R.id.item_disc_rel1_text2);//16位字母和数字组合

        item_disc_popup_text3.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        item_disc_popup_text3.getPaint().setAntiAlias(true);//抗锯齿

        if(!TextUtils.isEmpty(se)){
            item_disc_popup_eidt1.setText(se);
        }
        if(setTtype == 2){
            type = 2;
            item_disc_rel1_text1.setText("设置你的新密码");
            item_disc_rel1_text2.setText("请用8—16位字母和数字组合");
            item_disc_popup_eidt1.setText("");
            item_disc_popup_eidt1.setHint("新密码");
            item_disc_popup_eidt1.setKeyListener(new DigitsKeyListener() {
                @Override
                public int getInputType() {
                    return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
                }

                @NonNull
                @Override
                protected char[] getAcceptedChars() {
                    return "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
                }
            });
            item_disc_popup_eidt2.setHint("再次输入新密码");
            item_disc_popup_eidt2.setVisibility(View.VISIBLE);
            item_disc_popup_eidt2.setKeyListener(new DigitsKeyListener() {
                @Override
                public int getInputType() {
                    return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
                }

                @NonNull
                @Override
                protected char[] getAcceptedChars() {
                    return "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
                }
            });
            item_disc_popup_text3.setVisibility(View.GONE);
        }else{
            item_disc_rel1_text1.setText("设置你的账号");
            item_disc_rel1_text2.setText("请输入手机号");
            item_disc_popup_eidt1.setInputType(InputType.TYPE_CLASS_NUMBER);
            item_disc_popup_eidt1.setText("");
            item_disc_popup_eidt1.setHint("设置账号");
            item_disc_popup_eidt2.setText("");
            item_disc_popup_eidt2.setVisibility(View.GONE);
            item_disc_popup_text3.setVisibility(View.VISIBLE);
        }
        //注销账号
        item_disc_popup_text3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                pp.popupDismissCancle2("1");
            }
        });


        //确认
        item_disc_popup_text2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = item_disc_popup_eidt1.getText().toString().trim();
                if(!TextUtils.isEmpty(trim)){
                    if(type == 1){
                        type = 2;
                        trim_num = trim;
                        item_disc_rel1_text1.setText("设置你的新密码");
                        item_disc_rel1_text2.setText("请用8—16位字母和数字组合");
                        item_disc_popup_eidt1.setText("");
                        item_disc_popup_eidt1.setHint("新密码");
                        item_disc_popup_eidt2.setHint("再次输入新密码");
                        item_disc_popup_eidt1.setKeyListener(new DigitsKeyListener() {
                            @Override
                            public int getInputType() {
                                return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
                            }

                            @NonNull
                            @Override
                            protected char[] getAcceptedChars() {
                                return "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
                            }
                        });
                        item_disc_popup_eidt2.setKeyListener(new DigitsKeyListener() {
                            @Override
                            public int getInputType() {
                                return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
                            }

                            @NonNull
                            @Override
                            protected char[] getAcceptedChars() {
                                return "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
                            }
                        });
                        item_disc_popup_eidt2.setVisibility(View.VISIBLE);
                        item_disc_popup_text3.setVisibility(View.GONE);
                    }else{
                        String trim2 = item_disc_popup_eidt2.getText().toString().trim();
                        if(trim2.equals(trim)){
                            popupWindow.dismiss();
                            item_disc_popup_eidt2.setText("");
                            item_disc_popup_eidt1.setText("");
                            pp.popupDismissCancle(trim_num,trim);
                        }else{
                            ZXToastUtil.showToast("两次设置的密码不一致");
                        }
                    }
                }
            }
        });

        item_disc_popup_text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        item_disc_rel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        pp.popupShow();
    }

    public static void backgroundAlpha(float bgAlpha, Activity context) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }


    public interface PopuStatus {
        void popupShow();
        void popupDismissCancle(String se1,String se2);
        void popupDismissCancle2(String se1);
    }
}
